package carcassonne.server.websocket.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.log4j.Logger;

public class HearBeatHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(HearBeatHandler.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);

        if (evt instanceof IdleStateEvent event) {
            if (event.state() == IdleState.READER_IDLE) {
//                System.out.println("读空闲");
            } else if (event.state() == IdleState.WRITER_IDLE) {
//                System.out.println("写空闲");
            } else if (event.state() == IdleState.ALL_IDLE) {
                logger.info("close channel " + ctx.channel().id().asLongText());
                ctx.channel().close();
            }
        }
    }
}
